package com.net.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.net.model.Commodity;
import com.net.model.Provider;
import com.net.service.ProviderService;
import com.opensymphony.xwork2.ActionSupport;

@Component("Provider-*")
@Scope("prototype")
public class ProviderAction extends ActionSupport {
	private Provider provider;
	private ProviderService providerService;
	private List<Provider> providers;
	private List<Commodity> commodities;
	private String[] selectedList;
	private String cid;
	private String orderBy;
	private String keyword;

	public String add() throws Exception {
		// System.out.println(provider.getProvider_name()+"llllll");
		if (selectedList != null) {
			for (int i = 0; i < selectedList.length; i++) {
				System.out.println(Integer.parseInt(selectedList[i]));
				if (providers.get(Integer.parseInt(selectedList[i])).getProvider_name().equals("")) {

				} else {
					provider = providers.get(Integer
							.parseInt(selectedList[i]));
					provider.setCommodities(null);
					this.providerService.add(provider);
				}
			}
		}
		this.list();
		return "add";
	}

	public String delete() throws Exception {
		//System.out.println(provider.getCommodities().get(0).getCommodity_name()+"******&&&");
		provider = providerService.loadProviderById(provider.getId());
		provider.setCommodities(null);
		this.providerService.delete(provider);
		this.list();
		// System.out.println(provider.getId()+":"+provider.getprovider_name());
		return "delete";
	}

	public String deleteSelected() throws Exception {
		// System.out.println(selectedList.length+"  :8888888888888888");
		Provider cust = new Provider();
		if (selectedList != null) {
			for (int i = 0; i < selectedList.length; i++) {
				System.out.println(Integer.parseInt(selectedList[i]));
				cust.setId(Integer.parseInt(selectedList[i]));
				this.providerService.delete(cust);
			}
		}
		this.list();
		return "deleteSelected";
	}

	public String list() throws Exception {
		this.providers = providerService.getProviders();
		// System.out.println(providers.get(0).getShort_name()+"++++++++++++++");
		return "list";
	}

	public String load() {
		this.provider = this.providerService.loadProviderById(provider.getId());
		return "load";
	}

	public String update() {
		// System.out.println(provider.getId()+"#################");
		provider = providerService.loadProviderById(provider.getId());
		// System.out.println(provider.getId()+"#################");
		return "update";
	}

	public String updateCommit() throws Exception {
		// System.out.println(provider.getId()+"*****##########");
		providerService.update(provider);
		this.list();
		return "updateCommit";
	}

	public String search() {
		this.providers = providerService.searchList(cid, keyword, orderBy);
		return "search";
	}

	public Provider getProvider() {
		return provider;
	}

	public void setProvider(Provider provider) {
		this.provider = provider;
	}

	public ProviderService getProviderService() {
		return providerService;
	}

	@Resource(name = "providerService")
	public void setProviderService(ProviderService providerService) {
		this.providerService = providerService;
	}

	public List<Provider> getProviders() {
		return providers;
	}

	public void setProviders(List<Provider> providers) {
		this.providers = providers;
	}

	public String[] getSelectedList() {
		return selectedList;
	}

	public void setSelectedList(String[] selectedList) {
		this.selectedList = selectedList;
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

}
